package com.blate.dogxiao.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.util.Pair;

import com.blate.dogxiao.bean.CityInfo;
import com.blate.dogxiao.databinding.ItemCitySelectorCityItemBinding;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 城市选择适配器
 *
 * @author Az
 * on 2020/11/18
 */
public class CitySelectorAdapter
        extends SimpleDataRecyclerViewAdapter<Pair<String, CityInfo>, ItemCitySelectorCityItemBinding> {

    private final Comparator<Pair<String, CityInfo>> mCityComparator = new Comparator<Pair<String, CityInfo>>() {
        @Override
        public int compare(Pair<String, CityInfo> o1, Pair<String, CityInfo> o2) {
            String str1 = o1 == null ? null : o1.first;
            String str2 = o2 == null ? null : o2.first;
            if (str1 == null && str2 == null) {
                return 0;
            } else if (str1 == null) {
                return -1;
            } else if (str2 == null) {
                return 1;
            } else {
                //return Pinyin.toPinyin(str1, "").compareTo(Pinyin.toPinyin(str2, ""));
                return str1.compareTo(str2);
            }
        }
    };

    private final Map<String, Integer> mKeyIndex = new HashMap<>();
    private static final String KEYS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    @Override
    protected ItemCitySelectorCityItemBinding onCreateBinding(LayoutInflater inflater, ViewGroup parent, int viewType) {
        return ItemCitySelectorCityItemBinding.inflate(inflater, parent, false);
    }

    @Override
    public void onBindViewHolder(@NonNull SimpleDataHolder<Pair<String, CityInfo>, ItemCitySelectorCityItemBinding> holder, Pair<String, CityInfo> data) {
        holder.getBinding().tvCity.setText(data.second.cityName);
    }

    @Override
    public void putData(boolean refresh, List<Pair<String, CityInfo>> data) {
        super.putData(refresh, data);
        Collections.sort(getDataList(), mCityComparator);

        mKeyIndex.clear();
        String key = null;
        int keyCursor = 0;
        for (int i = 0; i < getDataList().size(); i += 1) {
            Pair<String, CityInfo> pair = getDataList().get(i);
            String currentKey = pair == null || TextUtils.isEmpty(pair.first) ? null : pair.first.substring(0, 1);
            if (currentKey != null && currentKey.matches("^[A-Z]")) {
                if (!Objects.equals(currentKey, key)) {
                    key = currentKey;
                    while (keyCursor < KEYS.length()) {
                        String expectKey = String.valueOf(KEYS.charAt(keyCursor++));
                        mKeyIndex.put(expectKey, i);
                        if (Objects.equals(expectKey, key)) {
                            break;
                        }
                    }
                }
            }
        }
    }

    public int getIndexByKey(String key) {
        Integer index = mKeyIndex.get(key);
        return index == null ? 0 : index;
    }

    public String getTag(int index) {
        if (getDataList().isEmpty()) {
            return null;
        }
        if (index < 0) {
            return null;
        }
        Pair<String, CityInfo> currentPair = getDataList().get(index);
        String currentKey = currentPair == null || TextUtils.isEmpty(currentPair.first) ? null : currentPair.first.substring(0, 1);
        if (index == 0) {
            return currentKey;
        }
        Pair<String, CityInfo> previousPair = getDataList().get(index - 1);
        String previousKey = previousPair == null || TextUtils.isEmpty(previousPair.first) ? null : previousPair.first.substring(0, 1);
        return Objects.equals(currentKey, previousKey) ? null : currentKey;
    }

    /**
     * 获取吸顶标签.
     *
     * @param firstTag 第一个标签
     * @return tag
     */
    public String getTopTag(String firstTag) {
        if (TextUtils.isEmpty(firstTag)) {
            return null;
        }
        int keyIndex = KEYS.indexOf(firstTag);
        return String.valueOf(KEYS.charAt(keyIndex <= 0 ? 0 : keyIndex - 1));
    }

    /**
     * 获取吸顶标签
     *
     * @param index 获取标签的view索引
     * @return 其实就是当前的条目属于的标签组
     */
    public String getTopTag(int index) {
        if (index < 0) {
            return null;
        }
        Pair<String, CityInfo> pair = getDataList().get(index);
        String currentKey = pair == null || TextUtils.isEmpty(pair.first) ? null : pair.first.substring(0, 1);
        if (currentKey != null && currentKey.matches("^[A-Z]")) {
            return currentKey;
        } else {
            return "#";
        }
    }

}
